<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:後臺登陸.
 *@param datetime 2011-10-18
 *@param author solgh
 ********************/
class Login extends CI_Controller{
  private $data=array();
  //跳轉介面
  public $referrer;

  function __construct(){
    parent::__construct();
    $this->load->model('admins_mdl');
    $this->data['title']='後臺登陸';
    $this->_check_referrer();
	$this->setLang();
    $this->data['lang'] =$this->lang->language;
  }

  function index(){
    $this->load->view("admins/login_form_view",$this->data);
  }

  /******
   * 检查referrer
   *
   * @access private
   * @return void
   */
  private function _check_referrer()
  {
    $ref = $this->input->get('ref', TRUE);
    $this->referrer = (!empty($ref)) ? str_replace('.html','',$ref) : '/admins/index';
    $this->referrer = (!empty($ref)) ? str_replace('.php','',$this->referrer) : '/admins/index';
  }

  //登出操作.
  public function login_out(){
    $nick_name=$this->nsession->userdata('nick_name');
    $this->operator_log('管理員登出>Name:'.$nick_name,'登出',1);
    $array_items = array('admin_id' => '', 'nick_name' => '', 'group_id' => '');
    $this->nsession->unset_userdata($array_items);
    redirect('admins/login');
  }
  /***
   * 登陸操作
   */
  public function  login_admin(){
      $this->validation_login();
      $authimg=$this->input->post('authcode');
      $authcode=$this->nsession->userdata('authcode');
      if($this->form_validation->run()!=false){

        if(strtoupper($authimg)==$authcode){ //驗證驗證碼是否正確.

          $query=$this->admins_mdl->check_admin(array(
            'login_name'=>$this->input->post('user_name'),
            'login_pwd'=>$this->input->post('user_pwd'),
            'status'=>1
          ));
          if($query){
            //$this->_login_log();
            $data=array('admin_id'=>$query['id'],'group_id'=>$query['group_id'],'nick_name'=>$query['login_name']);
            $this->nsession->set_userdata($data);
            $this->operator_log('管理員登入->Name:'.$query['login_name'],'登入',1);
            redirect($this->referrer);
          }else{
            $this->data['upload_error']='用戶名或者密碼錯誤,或帳戶被禁用!';
            $this->load->view("admins/login_form_view",$this->data);
          }
        }else{
            $this->data['upload_error']='對不起,驗證碼輸入錯誤!';
            $this->load->view("admins/login_form_view",$this->data);
        }
      }else{
        $this->data['error']=true;
        $this->load->view("admins/login_form_view",$this->data);
      }
  }
  //登錄日誌
  private function _login_log(){
    $login_path=FCPATH.'themes/login';
    if(!is_writable($login_path)){
     @chmod($login_path,0777);
    }
    @file_put_contents($login_path,time());
  }
  /****
   * 驗證登陸
   */
  private function validation_login(){
    $this->form_validation->set_rules('user_name','用戶名','trim|required|htmlspecialchars|max_length[32]');
    $this->form_validation->set_rules('user_pwd','密 碼','trim|required|max_length[20]|md5');
    $this->form_validation->set_rules('authcode','驗證碼','trim|required|max_length[4]');
  }

  //加載驗證碼
  public function load_auth(){
    $this->load->helper('auth');
    auth_img();
  }

  //操作日誌
  public function operator_log($title='',$action='',$result=1){
    $this->load->model('operator_mdl');
    $data=array(
      'operator'=>$this->nsession->userdata('nick_name'),
      'title'=>$title,
      'action'=>$action,
      'urls'=>$this->uri->uri_string(),
      'result'=>$result>0?'成功':'失敗',
      'add_time'=>time()
    );
    $this->operator_mdl->add_operator($data);
  }
  public function setLang(){
    $lan_get = 	$this->input->get('lan','');
    $lan_get = empty($lan_get) ? get_cookie('language') : $lan_get;
    if(empty($lan_get) || !in_array($lan_get,array('zh-tw','english'))){
        $lan_get = 'english';
    }
    $this->lang->load('page', $lan_get);
    set_cookie('language',$lan_get,time()+24*3600*30);
    //$this->data['getlang'] = $lan_get;
    $this->settings['lang'] =$this->lang->language;

  }
}
